package logic

import (
	"context"
	"encoding/json"
	"rent-ease/models/cache"
	"rent-ease/models/mysql"
	"time"

	"rent-ease/rentrpc/internal/svc"
	"rent-ease/rentrpc/rentrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseListLogic {
	return &HouseListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 房源列表
func (l *HouseListLogic) HouseList(in *rentrpc.HouseListRequest) (*rentrpc.HouseListResponse, error) {
	// todo: add your logic here and delete this line

	var list []*rentrpc.HouseList

	result, _ := cache.Rdb.Get(l.ctx, "list").Result()

	if result == "" {
		list, _ = mysql.HouseList(mysql.DB)

		marshal, _ := json.Marshal(list)

		cache.Rdb.Set(l.ctx, "list", marshal, time.Minute*30)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	return &rentrpc.HouseListResponse{
		List: list,
	}, nil
}
